<?php

namespace system\components;

/**
 * Base class for all {@link DocumentComponent}
 * holds all data that will be transformed to xml data
 * structure which will be represented visually using
 * XSL template associated with this component. Every
 * public variable will be transformed into XML tag.
 *
 * There two reserved variables names used as meta data
 * for concrete component: __componentName - holds name of concrete 
 * {@link DocumentComponent} and will be used as name for "root"
 * of this element
 *
 * __inner - holds array of {@link DocumentComponent}'s that will be processed
 * in a same way as this one
 *
 * @package system\components
 * @author Pavel Egorov {@link mailto:pavel@egorov.ru pavel@egorov.ru}
 * @version 0.0.1 $Revision: 50 $
 * @copyright Copyright (c) 2011, Pavel Egorov
 */
class DocumentComponent {

    /**
     * Name of this component used as root element for this component
     *
     * @var String $componentName
     */
    private $__componentName;

    /**
     * Constructor
     *
     * @param String $strName name of this component
     */
    public function __construct($strName) {
        $this->__componentName = $strName;
        if(!preg_match('#^[\\\_a-zA-Z]+[\\\_a-zA-Z0-9]*$#', $this->__componentName)){
            throw new \Exception('Invalid Component Name Exception');
        }
    }

    /**
     * Converts this object into array of variables
     *
     * @return array of this object variables
     */
    public function toArray() {
        $arr[$this->__componentName] = get_object_vars($this);
        return $arr;
    }

    /**
     * Adds another page component and associates or not template
     *
     * @param DocumentComponent $component component to be added
     */
    public function add($component,$templateName = NULL) {
        $this->__inner[] = $component;
        if($templateName != NULL){
            $this->__template = $templateName;
        }
    }

}